﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Pg.BioMedics.SDR.Entities;
using Pg.BioMedics.SDR.Services;
using Pg.BioMedics.SDR.Web.Controls;
using Pg.BioMedics.SDR.Web.Constants;

public partial class Operations_Views_SearchResultsView : BaseHostedView
{
    #region Fields

    private ISearchService searchService;

    private IList<SearchResult> searchResults;
    private IList<RankedDocument> documents;

    #endregion

    #region Properties

    /// <summary>
    /// Sets initialized instance of indexing service. This property 
    /// is utilized by IoC container
    /// </summary>
    public ISearchService SearchService
    {
        set
        {
            this.searchService = value;
        }
    }

    #endregion

    #region Event handlers

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (this.searchResults != null)
            {
                // extract documents
                this.documents = (from searchResult in this.searchResults
                                  let document = this.searchService.GetDocument(searchResult)
                                  select new RankedDocument()
                                  {
                                      RelevanceScore = searchResult.RelevanceScore,
                                      Id = document.Id,
                                      OriginalId = document.OriginalId,
                                      Title = document.Title,
                                      Description = document.Description,
                                      ContentId = document.ContentId,
                                      Reference = document.Reference,
                                      TypeCode = document.TypeCode,
                                      LanguageCode = document.LanguageCode,
                                      StatusCode = document.StatusCode,
                                      StatusDate = document.StatusDate,
                                      StatusAuthorLogin = document.StatusAuthorLogin
                                  }).ToList();


                // bind documents
                this.searchResultsGridEX.DataSource = this.documents;
                this.searchResultsGridEX.DataBind();
            }
        }
        catch (Exception ex)
        {
            base.Navigate(DocumentsViews.ErrorView, ReturnViewName, ex);
        }
    }

    protected override void LoadParameters(object[] parameters)
    {
        if (parameters.Length == 1)
        {
            if (parameters.First() is IList<SearchResult>)
                this.searchResults = parameters.First() as IList<SearchResult>;
        }
    }

    protected void ShowDocumentDetails(object sender, EventArgs e)
    {
        base.Navigate(DocumentsViews.DocumentDetailsView, DocumentsViews.SearchResultsView, GetSelectedDocument());
    }

    #endregion

    #region Support methods

    private Document GetSelectedDocument()
    {
        int documentId = (int)searchResultsGridEX.GetRow().Cells["Id"].Value;
        return (from searchResult in base.Parameters.First() as IList<SearchResult>
                let document = this.searchService.GetDocument(searchResult)
                where (document.Id.HasValue) && (document.Id.Value == documentId)
                select document).FirstOrDefault();
    }

    #endregion
}

public class RankedDocument : Document
{
    public double RelevanceScore
    {
        get;
        set;
    }
}
